﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MakeIT_Bill_Maker
{
    public partial class frmEditFixedChargers : Form
    {
        // Data Connection
        private Database.DatabaseConnection db = new Database.DatabaseConnection();
        private DataSet ds;
        private string sql;
        private DataTable dt;

        public frmEditFixedChargers()
        {
            InitializeComponent();
        }

        // Form Load
        private void frmEditFixedChargers_Load(object sender, EventArgs e)
        {
            sql = "SELECT ServiceCharge,Welfare FROM OtherFixed WHERE Index=1";
            ds = db.Show(sql);
            dt = ds.Tables[0];
            foreach (DataRow row in dt.Rows)
            {
                txtService.Text = row[0].ToString();
                txtWelfare.Text = row[1].ToString();
            }
            
        }

        // 'Close' button
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            clear_All();
        }

        // 'Edit' button
        private void btnEdit_Click(object sender, EventArgs e)
        {
            double out1, out2;
            string service = txtService.Text;
            string welfare = txtWelfare.Text;
            if(!double.TryParse(service, out out1))
            {
                MessageBox.Show("'Penalty Rate' must be a number only.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (!double.TryParse(welfare, out out2))
            {
                MessageBox.Show("'Welfare Charge' must be a currency value.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (out1 < 0 || out2 < 0)
            {
                MessageBox.Show("Enter positive values for chargers.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                sql = "UPDATE OtherFixed SET ServiceCharge = '" + service + "',Welfare = '" + welfare + "' WHERE Index=1";
                bool b = db.edit(sql);
                if (b == true)
                {
                    MessageBox.Show("Value(s) updated succesfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Failed to update the value(s).", "Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        // Method to clear the window 
        private void clear_All()
        {
            txtService.Clear();
            txtWelfare.Clear();         
        }
    }
}
